import time

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy


class CommonDriver:
    def get_driver(self):
        desc = {
            'platformName': 'Android',
            'platformVersion': '7',
            'appPackage': 'com.dangdang.buy2',
            'appActivity': 'com.dangdang.buy2.StartupActivity',
            'noReset': False,
            'unicodeKeyboard': True,
            'udid': '127.0.0.1:21513',
        }

        command_executor_url = 'http://127.0.0.1:4723/wd/hub'

        driver = webdriver.Remote(command_executor=command_executor_url, desired_capabilities=desc)

        driver.implicitly_wait(10)

        print("连接成功")

        return driver


if __name__ == '__main__':
    driver = CommonDriver().get_driver()
    # driver.find_element(AppiumBy.ID,value='com.dangdang.buy2:id/tv_sure').click()
    # driver.find_elements(AppiumBy.CLASS_NAME,'android.widget.TextView')[3].click()
    # # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"and @text="同意"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.LinearLayout/android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_dis"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/dialog_cancel_tv"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.FrameLayout[@resource-id="com.dangdang.buy2:id/home_title_layout"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]').send_keys("浪潮之巅")
    # driver.find_element(AppiumBy.XPATH,'//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]').clear()
    # time.sleep(3)
    # driver.quit()

    # try:
    #     screen_size = driver.get_window_size()
    #     width = screen_size["width"]
    #     print("------",width)
    #     height = screen_size["height"]
    #
    #     start_x = width/ 2
    #     start_y = height * 0.8
    #     end_x = width/ 2
    #     end_y = height * 0.2
    #     print(start_x,start_y,end_x,end_y)
    #
    #     driver.swipe(start_x,start_y.end_x,end_y)
    #     time.sleep(2)
    # finally:
    #     driver.quit()


